using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HandyIRCChat
{
    public partial class PrivateForm : Form
    {
        private HandyLogic handyLogic;

        private PrivateObject privateObj;

        private delegate void SetOutputMessage(string text);

        private delegate void CloseFormDelegate();

        public PrivateForm(HandyLogic hl, PrivateObject obj)
        {
            handyLogic = hl;
            privateObj = obj;
            privateObj.PrivateForm = this;

            InitializeComponent();

            privateIn.KeyDown += new KeyEventHandler(OnPrivateInKeyDown);
            privateObj.PrivateMessageEvent += 
                new PrivateObject.PrivateMessageEventHandler(PrivateObj_QueryMessage);

            handyLogic.ClosePrivateFormEvent += 
                new HandyLogic.ClosePrivateFormEventHandler(handyLogic_ClosePrivateFormEvent);

            if (privateObj.MessageBuffer.Length != 0)
                WriteOutputMessage(privateObj.MessageBuffer);

            this.Text = privateObj.UserName;

            privateIn.Focus();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            privateIn.Focus();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            privateObj.PrivateMessageEvent -=
                new PrivateObject.PrivateMessageEventHandler(PrivateObj_QueryMessage);

            privateObj.PrivateForm = null;

            handyLogic.Options.PrivateWindowState.LocX = this.Location.X;
            handyLogic.Options.PrivateWindowState.LocY = this.Location.Y;
            handyLogic.Options.PrivateWindowState.SizeX = this.Size.Width;
            handyLogic.Options.PrivateWindowState.SizeY = this.Size.Height;

            base.OnClosing(e);
        }

        protected override void OnCreateControl()
        {
            this.SetBounds(handyLogic.Options.ChannelWindowState.LocX,
                handyLogic.Options.PrivateWindowState.LocY,
                handyLogic.Options.PrivateWindowState.SizeX,
                handyLogic.Options.PrivateWindowState.SizeY);

            base.OnCreateControl();
        }

        private void WriteOutputMessage(string text)
        {
            if (this.privateOut.InvokeRequired)
            {
                SetOutputMessage d = new SetOutputMessage(WriteOutputMessage);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                privateOut.AppendText(text);
                privateOut.ScrollToCaret();

                if (!this.ContainsFocus)
                    this.Activate();
            }
        }

        private void CloseForm()
        {
            this.Close();
        }

        private void PrivateObj_QueryMessage(object sender, string message)
        {
            WriteOutputMessage(message);
        }

        private void OnPrivateInKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (privateIn.Text.Length == 0)
                    return;

                StringBuilder sb = new StringBuilder(privateIn.Text);
                if (sb[0] == '\n')
                    sb.Remove(0, 1);

                privateObj.SendMessage(sb.ToString());
                privateIn.Clear();
            }
        }

        private void handyLogic_ClosePrivateFormEvent(object sender, string userName)
        {
            if (this.InvokeRequired)
            {
                if (userName == privateObj.UserName)
                {
                    CloseFormDelegate d = new CloseFormDelegate(CloseForm);
                    this.Invoke(d, new object[] { });
                }
            }
            else
                this.Close();
        }
    }
}
